package com.maixin.code.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.maixin.code.base.BaseAction;
import com.maixin.code.entity.UserWorkTimeInfo;
import com.maixin.code.service.UserWorkTimeInfoService;

public class UserWorkTimeAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private UserWorkTimeInfo userWorkTimeInfo;
	private UserWorkTimeInfoService userWorkTimeSer;
	private List<UserWorkTimeInfo> list;
	private Integer cnt;
	private static final int BUFFER_SIZE = 16 * 1024;  
	private File upload;  
    private String uploadFileName;   //fileName 前面必須和uplaod一致,不然找不到文件  
	/**
	 * 查询所有的工作时间
	 * @return
	 */
	public String findAllWorkTime()throws Exception{
		
		list = userWorkTimeSer.getBaseDao().findAll(UserWorkTimeInfo.class, start, limit);
		cnt = userWorkTimeSer.getBaseDao().findTabCnt(UserWorkTimeInfo.class);
		return SUCCESS;
	}
	
	/**
	 * 上传Excel
	 * @return
	 */
	public String uploadExcel(){
		try {
			Date d = new Date();  
	        System.out.println("uploadFileName = "+this.uploadFileName);  
	        //upload --  wapps 下面的文件夹,用来存放图片  
	        String toSrc = ServletActionContext.getServletContext().getRealPath("upload")+"/"+d.getTime()+getFileExp(this.uploadFileName);  //使用時間戳作為文件名  
	        System.out.println("toFile= "+toSrc);  
	        File toFile = new File(toSrc);  
	        writeFile(this.upload,toFile);  
	        userWorkTimeSer.saveExcelWorkTime(upload);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return SUCCESS;  
	}
	
	// 上传文件的文件名  
    private String getFileExp(String name) {  
        int pos = name.lastIndexOf(".");  
        return name.substring(pos);  
    }  
	
    private static void writeFile(File src, File dst) {  
        System.out.println(" == 文件写入 == ");  
        try {  
            InputStream in = null;  
            OutputStream out = null;  
            try {  
                in = new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE);  
                out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);  
                byte[] buffer = new byte[BUFFER_SIZE];  
                while (in.read(buffer) > 0) {  
                    out.write(buffer);  
                }  
            } finally {  
                if (null != in) {  
                    in.close();  
                }  
                if (null != out) {  
                    out.close();  
                }  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        System.out.println("写入成功！");  
    }  
	
	
	
	
	
	public UserWorkTimeInfo getUserWorkTimeInfo() {
		return userWorkTimeInfo;
	}
	public void setUserWorkTimeInfo(UserWorkTimeInfo userWorkTimeInfo) {
		this.userWorkTimeInfo = userWorkTimeInfo;
	}
	public UserWorkTimeInfoService getUserWorkTimeSer() {
		return userWorkTimeSer;
	}
	public void setUserWorkTimeSer(UserWorkTimeInfoService userWorkTimeSer) {
		this.userWorkTimeSer = userWorkTimeSer;
	}
	public List<UserWorkTimeInfo> getList() {
		return list;
	}
	public void setList(List<UserWorkTimeInfo> list) {
		this.list = list;
	}
	public Integer getCnt() {
		return cnt;
	}
	public void setCnt(Integer cnt) {
		this.cnt = cnt;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
}
